﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
using UnityEngine.UI;
public class Level7X9Letters : MonoBehaviour
{
    public GameLevelItem gameLevelItem;
    public List<Transform> loveRootList = new List<Transform>();
    public List<TextButton> buttonList = new List<TextButton>();
    public List<Transform> loveLetterList = new List<Transform>();
    public GridLayoutGroup glg;
    public List<bool> stateList = new List<bool>();
    public Color activeColor;
    private bool result = false;

    void Start()
    {
        InitBtnEvent();
    }

    private void InitBtnEvent()
    {
        int count = buttonList.Count;
        for (int i = 0; i < count; i++)
        {
            int tempIndex = i;
            if (!UConst.Str_TargetLove.Contains(buttonList[tempIndex].text.text))
                buttonList[tempIndex].text.text = string.Empty;
            buttonList[tempIndex].button.onClick.AddListener(() =>
            {
                SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
                BtnClick(tempIndex);
            });
            stateList.Add(false);
        }
    }

    private void BtnClick(int index)
    {
        if (result)
            return;
        if (stateList[index]) return;
        stateList[index] = true;
        int tempIndex = 0;
        // 左
        tempIndex = index - 1;
        if (tempIndex >= 0 && tempIndex / 7 == index / 7) // 前一个方块在同一行
            stateList[tempIndex] = true;
        // 右
        tempIndex = index + 1;
        if (tempIndex < 63 && tempIndex / 7 == index / 7)  // 后一个方块在同一行
            stateList[tempIndex] = true;
        // 上
        tempIndex = index - 7;
        if (tempIndex >= 0)
            stateList[tempIndex] = true;
        // 下
        tempIndex = index + 7;
        if (tempIndex < 63)
            stateList[tempIndex] = true;
        UpdateImageColor();
    }

    private void UpdateImageColor()
    {
        bool tempResult = true;
        int count = buttonList.Count;
        for (int i = 0; i < count; i++)
        {
            if (stateList[i])
            {
                buttonList[i].button.image.color = activeColor;
                if (!string.IsNullOrEmpty(buttonList[i].text.text))
                {
                    gameLevelItem.TriggerResult(false);
                    result = true;
                }
            }
            else
            {
                if (string.IsNullOrEmpty(buttonList[i].text.text))
                {
                    tempResult = false;
                }
            }
        }

        if (tempResult)
        {
            result = true;
            for (int i = 0; i < count; i++)
            {

                if (string.IsNullOrEmpty(buttonList[i].text.text))
                {
                    buttonList[i].button.image.DOFade(0, 0.5f);
                }
            }
            DoWinAnimation();
        }
    }

    private void DoWinAnimation()
    {
        if (loveLetterList.Count == loveLetterList.Count)
        {
            int count = loveLetterList.Count;
            for (int i = 0; i < count; i++)
            {
                loveLetterList[i].transform.parent = this.transform;
                loveLetterList[i].transform.DOMove(loveRootList[i].transform.position, 0.5f);
            }
        }
        Invoke("TriggerResultWin", 0.7f);
    }

    private void TriggerResultWin()
    {
        gameLevelItem.TriggerResult(true);
    }


}
